﻿using CefSharp;
using CefSharp.WinForms;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DChrome.Windows
{
    public partial class MaiForm : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        public MaiForm()
        {
            Load += Form_Load;
           
            InitializeComponent();
        }

        protected void Form_Load(object sender,EventArgs e)
        {
            InitializeChromium();
        }
        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();

            settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.0.0.13547";
            Cef.Initialize(settings);
            // Initialize cef with the provided settings
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("https://www.2345.com/?k2156545");
            // Add it to the form and fill it to the form window.
            Panal.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
            ////////////执行js/////////////////////////////////  
            //chromeBrowser.IsBrowserInitializedChanged += OnIsBrowerInitializedChanged;//添加事件 
            chromeBrowser.FrameLoadEnd += OnIsFramLoadEnd;
           
        }
        private void OnIsFramLoadEnd(object sender,FrameLoadEndEventArgs e)
        {
            Thread.Sleep(TimeSpan.FromSeconds(100));
            Dispose();
            System.Environment.Exit(0);
        }
        private void OnIsBrowerInitializedChanged(object sender,IsBrowserInitializedChangedEventArgs e)
        {
            if (e.IsBrowserInitialized)
            {
                //chromeBrowser.ExecuteScriptAsync("alert(1)");
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }
    }
}
